package com.shaoshaohuo.app.ui;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.UserSignEntity;
import com.shaoshaohuo.app.entity.UserSignEntity.SignData;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.calendar.CalendarCard;
import com.shaoshaohuo.app.view.calendar.CalendarCard.OnCellClickListener;
import com.shaoshaohuo.app.view.calendar.CustomDate;
import com.shaoshaohuo.app.utils.DeviceHelper;

import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 签到详情页
 * 
 * @author cao
 *
 */
public class UserSignActivity extends BaseActivity implements OnCellClickListener {

	private int screenWidth;
	private final static double IMAGE_WIDTH = 1080.00;
	private final static double IMAGE_HEIGHT = 560.00;

	private TopbarView mTopbarView;
	private LinearLayout imageview;
	private TextView tv_month_point;
	private TextView tv_next_point;
	private TextView tv_data;
	private CalendarCard calendarCard;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_sign);
		initView();
		setUpView();
		requestData(true);
	}

	private void initView() {
		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);
		initLayout();
		LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll);
		calendarCard = new CalendarCard(this, this);
		linearLayout.addView(calendarCard);

		tv_month_point = (TextView) findViewById(R.id.tv_month_point);
		tv_next_point = (TextView) findViewById(R.id.tv_next_point);
		tv_data = (TextView) findViewById(R.id.tv_data);
	}

	private void setUpView() {
		mTopbarView.setCenterText("我的签到");
		mTopbarView.setLeftView(true, true);
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
		tv_data.setText(format.format(new Date()));
	}

	private void requestData(boolean isLoading) {
		startLoadingDialog();
		RequestService.getInstance().userSign(this, UserSignEntity.class, new RequestListener() {
			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				dismissLoadingDialog();
				if (resultData.isOk()) {

					UserSignEntity signEntity = (UserSignEntity) resultData;
					SpannableString text = new SpannableString("本月累计签到：" + signEntity.data.point + "分");
					text.setSpan(new AbsoluteSizeSpan(13, true), 0, 7, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
					text.setSpan(new AbsoluteSizeSpan(13, true), text.length() - 1, text.length(),
							SpannableString.SPAN_EXCLUSIVE_INCLUSIVE);
					tv_month_point.setText(text);
					tv_next_point.setText("明日签到+" + signEntity.data.NextDayPoint);

					List<SignData> list = signEntity.data.list;
					int[] days = new int[list.size()];
					for (int i = 0; i < list.size(); i++) {
						String[] split = list.get(i).cdate.split("-");
						if (split[2].startsWith("0"))
							split[2] = split[2].substring(1);
						days[i] = Integer.parseInt(split[2]);
					}
					calendarCard.setSignDays(days);

//					if ("0".equals(signEntity.Data.isSign)) {
//						SharedPreferencesHelper.setInt(Field.SIGN_POINT, signEntity.Data.NextDayPoint);
//						SharedPreferencesHelper.setLong(UserSignActivity.this, Field.SIGN_TIME,
//								System.currentTimeMillis());
//
//					}

				} else {
					showToast(resultData.getMsg());
				}
			}

			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				dismissLoadingDialog();
				showToast(msg);
			}
		});

	}

	private void initLayout() {
		imageview = (LinearLayout) findViewById(R.id.linearLayout);
		screenWidth = DeviceHelper.getScreenWidth(this);
		double scale = IMAGE_HEIGHT / IMAGE_WIDTH;
		int height = (int) (screenWidth * scale);
		LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) imageview.getLayoutParams();
		layoutParams.height = height;
		imageview.requestLayout();
	}

	@Override
	public void clickDate(CustomDate date) {
		// TODO Auto-generated method stub

	}

	@Override
	public void changeDate(CustomDate date) {
		// TODO Auto-generated method stub

	}

}
